<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
        }
        .box{
            position: absolute;
            top: 0;
            left: 0;
            width: 100px;
            height: 100px;
            background-color: pink;
        }
    </style>
</head>
<body>
    <div class="box"></div>

    <script>
        var box = document.querySelector('.box');
        box.onmousedown = function (e) {
            var offsetX = e.offsetX;
            var offsetY = e.offsetY;

            document.onmousemove = function (e) {
                console.log(e);
                var currentX = e.pageX;
                var currentY = e.pageY;
                box.style.left = currentX - offsetX + 'px';
                box.style.top = currentY - offsetY + 'px';
            }
            box.onmouseup = function () {
                document.onmousemove = null;
            }
        }
    </script>
</body>
</html>